﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class MainForm
    {
        /// <summary>
        /// Initiates the procedure of adding new holidays
        /// </summary>
        /// <param Name="pending">
        /// is this just a request (pending) or a confirmed _holiday (non-pending)? no Value leaves Open choice
        /// </param>
        private void AddHolidays(bool? pending)
        {
            Int64 empID;
            if (Program.Logged.CanEditHolidays)
            {
                Program.ChooseEmployee =
                    new ForWhom(
                        Resources.FORM_FORWHOM_Title_Holidays,
                        (from _element in dgridHolidayEmployees.DataSource as List<EmployeeAllowance>
                         select _element.ID).ToArray());
                if (Program.ChooseEmployee.ShowDialog() == DialogResult.OK)
                {
                    empID = Program.ChooseEmployee.GetID;
                }
                else
                {
                    return;
                }                
            }
            else
            {
                if (Program.Logged.NotAnEmployee)
                {
                    MessageBox.Show(
                        Resources.MESSAGEBOX_ERROR_AddingHolidaysSomebodyElseAttempted_MESSAGE,
                        Resources.MESSAGEBOX_ERROR_AddingHolidaysSomebodyElseAttempted_CAPTION,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return;
                }                
                empID = Program.Logged.ID;                
            }
            Program.holidayForm = new HolidayDetails(
                empID,
                SQLiteAccess.SpawnHolidaysRow(),
                InputAction.Create,
                pending);
            if (Program.holidayForm.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            Hourglass(true);
            SQLiteAccess.AddNewHoliday(
                empID,
                Program.holidayForm.Holiday);
            Sounds.Play(Sounds.Validated); 
            Hourglass(false);
        }

        internal void EditHolidays(Int64 id, bool? pending)
        {
            var row = SQLiteAccess.GetHolidays(id);
            Program.holidayForm = new HolidayDetails(
                (Int64)row[1],
                row,
                InputAction.Update,
                pending);
            if (Program.holidayForm.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            Hourglass(true);
            SQLiteAccess.UpdateHolidays(id, Program.holidayForm.Holiday);
            Sounds.Play(Sounds.Validated); 
            Hourglass(false);
        }

        private void DeleteHolidays(Int64 id)
        {
            var userSure = MessageBox.Show(
                Resources.MESSAGEBOX_QUESTION_DeleteHolidays_MESSAGE,
                Resources.MESSAGEBOX_QUESTION_DeleteHolidays_CAPTION,                
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);
            if (userSure == DialogResult.No) return;

            Hourglass(true);
            SQLiteAccess.DeleteHoliday(id);
            Sounds.Play(Sounds.Validated);
            Hourglass(false);
        }

        private void SwitchPendingHoliday(bool pNewPendingValue)
        {
            Hourglass(true);
            var row = SQLiteAccess.GetHolidays(lvHolidaysPending.SelectedTag.id);
            if (row == null)
            {
                Hourglass(false);
                return;
            }
            row[5] = pNewPendingValue;
            SQLiteAccess.UpdateHolidays(
                (Int64)row[0],
                row);
            Sounds.Play(Sounds.Validated); 
            Hourglass(false);
        }

        private void ManageHolidays(Int64 pEmployeeID)
        {
            HolidaysTab_SelectEmployee(pEmployeeID);
            Sounds.Play(Sounds.Pageturn);
        }
    }
}
